package com.ruoze.bdp.enums;


import static java.util.stream.Collectors.toMap;

import java.util.Arrays;
import java.util.Map;
import java.util.NoSuchElementException;

public enum DbType {
    MYSQL(0, "mysql"),
    HIVE(1, "hive"),
    HBASE(2, "hbase"),
    DORIS(3, "doris"),
    CLICKHOUSE(4, "clickhouse")
    ;

    private final int code;
    private final String desc;

    DbType(int code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public int getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

    private static final Map<Integer, DbType> DB_TYPE_MAP = Arrays.stream(DbType.values()).collect(toMap(DbType::getCode, dbtype -> dbtype, (k1, k2) -> k1));

    public static DbType of(int type) {
        if (DB_TYPE_MAP.containsKey(type)) {
            return DB_TYPE_MAP.get(type);
        }
        return null;
    }

    public static DbType ofName(String name) {
        return Arrays.stream(DbType.values()).filter(e -> e.name().equals(name)).findFirst().orElseThrow(() -> new NoSuchElementException("no such db type"));
    }

    public boolean isHive() {
        return this == DbType.HIVE;
    }
}
